package com.atguigu.boot.dto;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class Result {

    private Boolean success;
    private Integer code;
    private String message;
    // 要返回给前端的数据
    private Map<String,Object> data = new HashMap<>();

    // 构造器私有化
    private Result(){}

    // 通用返回成功
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.SUCCESS.getSuccess());
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return result;
    }

    // 通用返回失败
    public static Result error(){
        Result result = new Result();
        result.setSuccess(ResultCodeEnum.UNKNOWN_ERROR.getSuccess());
        result.setCode(ResultCodeEnum.SUCCESS.getCode());
        result.setMessage(ResultCodeEnum.UNKNOWN_ERROR.getMessage());
        return result;
    }

    // 设置结果
    public static Result setResult(ResultCodeEnum resultCodeEnum){
        Result result = new Result();
        result.setSuccess(resultCodeEnum.getSuccess());
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    ///////// 链式编程，返回类本身 ////////////
    // 自定义返回数据
    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }

    // 通用设置返回的数据
    public Result data(String key,Object value){
        this.data.put(key, value);
        return this;
    }

    // 自定义响应结果标识
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    // 自定义响应结果状态码
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    // 自定义状态码描述信息
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
}
